﻿using System;
using Dapasoft.Internal.MVP.Model;
using Telerik.OpenAccess;
using Telerik.OpenAccess.Query;

namespace Dapasoft.Internal.MVP.Presenter
{
    /// <summary>
    /// ScopePresenter class is a presenter of the Telerik ORM model. 
    /// </summary>
    public class ScopePresenter : PresenterBase, IDisposable
    {
        #region Fields

        /// <summary>
        /// Database scope.
        /// </summary>
        private IObjectScope scope = NorthwindProvider.GetNewObjectScope();

        #endregion // Fields

        #region Constructors

        /// <summary>
        /// Enables a server control to perform final clean up before it is released from memory.
        /// </summary>
        public void Dispose()
        {
            scope.Dispose();
        }

        #endregion // Constructors

        #region Methods

        /// <summary>
        /// Gets the filtered data.
        /// </summary>
        /// <param name="selectedValue">The selected value.</param>
        /// <returns>Object, contains the filtered data.</returns>
        public override object GetFilteredData(int? selectedValue)
        {
            var filteredData = from product in scope.Extent<Product>()
                   where product.Category.CategoryID == selectedValue
                   select new
                   {
                       Product = product.ProductName,
                       QuantityPerUnit = product.QuantityPerUnit,
                       Category = product.Category.CategoryName
                   };

            return filteredData.ToList();
        }

        #endregion // Methods
    }
}
